#! /bin/sh

# $Id$

load_progpath() {
	AWK=`which awk`
	CAT=`which cat`
	CUT=`which cut`
	DATE=`which date`
	GREP=`which grep`
	HEAD=`which head`
	TAIL=`which tail`
	ID=`which id`
	LDAPSEARCH=`which ldapsearch`
	PS=`which ps`
	SED=`which sed`
	SLEEP=`which sleep`
	WHO=`which who`
	QUOTA=`which quota`

	return 0
}

show_bar() {
	local _percent _tempvar
	_percent=$1

	_tempvar="0"

	echo -e "\e[41m\c"
	while [ ${_tempvar} -le "100" ]; do
		if [ ${_tempvar} -lt ${_percent} ]; then
			echo -n " "
		else
			echo -e "\e[46m \c"
		fi
		_tempvar=`expr ${_tempvar} + 2`
	done
	echo -e "\e[0m\c"

	return 0
}

show_disk() {
	echo -e "\e[1;33m= Disk Usage =================================================================\e[0m"

	# `quota` shows local names of nodes in FreeBSD & Solaris,
	# but shows remote name in Linux.
	if [ "${OS}" = "Linux" ]; then
		QUOTA="${QUOTA} -Qw"
		HOMEKEY="home"	# cshome
	fi	

	QINFO=`${QUOTA}`

	MQ=`echo "${QINFO}" | ${GREP} ${MAILKEY}`
	MQ_FULL=`echo ${MQ} | ${AWK} '{print $3}'`
	MQ_USED=`echo ${MQ} | ${AWK} '{print $2}' | ${AWK} -F'*' '{print $1}'`

	if [ -n "${MQ_FULL}" ] && [ ${MQ_FULL} -gt "0" ]; then
		UPCERNT=`expr ${MQ_USED} \* 100 / ${MQ_FULL}`

		echo -e " \e[1mMail:\e[0m \c"
		show_bar "${UPCERNT}"

		if [ ${UPCERNT} -lt "5" ] || [ ${UPCERNT} -gt "95" ]; then
			echo " ${UPCERNT}% ${MQ_USED}/${MQ_FULL} KB"
		else
			echo " ${UPCERNT}%" `expr ${MQ_USED} / 1000`/`expr ${MQ_FULL} / 1000` MB
		fi

		if [ ${UPCERNT} -gt "100" ]; then
			echo -e "\e[1;5;31mBoom!!\e[0m \e[1;33m Your Mailbox is out of quota limited! Reduce it immediately!\e[0m"
		else
			echo ""
		fi
	fi

	HQ=`echo "${QINFO}" | ${GREP} ${HOMEKEY} | ${HEAD} -n 1`

	HQ_FULL=`echo ${HQ} | ${AWK} '{print $3}'`
	HQ_USED=`echo ${HQ} | ${AWK} '{print $2}' | ${AWK} -F'*' '{print $1}'`

	if [ -n "${HQ_FULL}" ] && [ ${HQ_FULL} -gt "0" ]; then
		echo -e " \e[1mHome:\e[0m \c"

		UPCERNT=`expr ${HQ_USED} \* 100 / ${HQ_FULL}`
		show_bar "${UPCERNT}"

		if [ ${UPCERNT} -lt "5" ] || [ ${UPCERNT} -gt "95" ]; then
			echo " ${UPCERNT}% ${HQ_USED}/${HQ_FULL} KB"
		else
			echo " ${UPCERNT}%" `expr ${HQ_USED} / 1000`/`expr ${HQ_FULL} / 1000` MB
		fi

		if [ ${UPCERNT} -gt "100" ]; then
			echo -e "\e[1;5;31mBoom!!\e[0m \e[1;33m Your HomeDir is out of quota limited! Reduce it immediately!\e[0m"
		fi
	fi
}

# Load all program path
load_progpath

# Global Variable
ETCPASSWD="/etc/passwd"
GID=`${ID} -g`
MAXTRIES="5"
MOTD="/net/admin/etc/motd"
SHELL=""
HOSTNAME=`$(which hostname)`
OS=`$(which uname)`
TRIES="0"
HOMEKEY="account"
MAILKEY="mail"

# If it's system account, don't use LDAP information
if [ -n "`${GREP} ^${USER}: ${ETCPASSWD}`" ]; then
        SHELL=`${AWK} -F: "{if (\\\$1 == \"${USER}\") print \\\$7}" ${ETCPASSWD}`
	if [ $# -gt 0 ]; then
		exec ${SHELL} $1 "$2"
	elif [ "${SHELL}" != "/bin/sh" ]; then
	# Login shell uses "-l", except sh
		exec ${SHELL} -l
	else
		exec ${SHELL}
	fi
fi

# Find its real shell
if [ "${HOSTNAME}" != "alumni" -a "${GID}" -eq "600" ]; then
	SHELL="/usr/sbin/nologin"
else
	while [ -z "${SHELL}" ] && [ "${TRIES}" -lt "${MAXTRIES}" ]; do
		SHELL=`${LDAPSEARCH} -LLL -x "uid=${USER}" loginShell | ${GREP} ^loginShell | ${SED} -e 's,^loginShell: ,,'`
		HOME=`${LDAPSEARCH} -LLL -x "uid=${USER}" homeDirectory | ${GREP} ^homeDirectory | ${SED} -e 's,^homeDirectory: ,,'`
		TRIES=`expr ${TRIES} + 1`
	done
fi

# If the shell is 'nologin'
if [ "${SHELL}" = "/usr/local/bin/cs.nologin" -a -e "${HOME}/.stop-message" ]; then
	${CAT} ~/.stop-message
	${SLEEP} 5
	exit 0
fi

# If it is a login shell (no arguments)
if [ $# -eq 0 ]; then
	# Show cscc announce, disk usage and other useful informations
	if [ -f ${MOTD} ]; then
		${CAT} ${MOTD}
	fi

	show_disk

	echo -e "\e[1;33m= Process ====================================================================\e[0m"
	${PS}
	echo -e "\e[0m\c"
	echo -e "\e[1;33m= Information ================================================================\e[0m"
	echo -e " \e[1mCurrent Time:\e[0m "`${DATE}`
	echo -e " \e[1mOnline Users:\e[0m "`${WHO} -q | ${TAIL} -n 1 | ${AWK} -F'=' '{print $2}'`" users"
	echo ""
	echo -e "                                 \e[1mCS Computer Center\e[m <\e[1mhelp@cs.nctu.edu.tw\e[0m>"

	# Login shell uses "-l", except sh
	if [ "${SHELL}" != "/bin/sh" ]; then
		exec ${SHELL} -l
	else
		exec ${SHELL}
	fi

# Non-interactive shell below
else
	exec ${SHELL} $1 "$2"
fi	# Login shell or not end
